Binary Number System Tutorial

Curt M. White
© 2003

Table of Contents

1. Base 10 number system
2. Base 2 number system
3. Base 16 number system
4.Conversion from one number system to another
5. Binary addition
6. Integer representation and two’s complement
7. Maximum values
8. Floating point representation
9. Character encoding (ASCII and EBCDIC)
10. Practice problems

Base 10 Number System (Decimal)

To get a good understanding of other number systems, let’s begin with a familiar topic - the base 10 number system.  In order to represent the base 10 system, we need 10 different symbols.  The symbols we use of course are the digits 0 through 9.  If we were to write a value using base 10, it would look like the following:

		302710

The subscript 10 has been placed on the end of the value so that there is no doubt that we are working with a base 10 value.

Breaking the value apart one digit at a time, the 7 is in the 1s position and is equal to 7 x 100, or simply 7 x 1 (anything to the 0th power is 1); the 2 is in the 10s position and is equal to 2 x 101, or 20; the 0 is in the 100s position and is equal to 0 x 102, or 0 x 100; and the 3 is in the 1000s position and is equal to 3 x 103, or 3 x 1000.  Note that starting on the right, the powers start at 0 and increment by 1 for every place we move to the left.

Base 2 Number System (Binary)

The binary number system, or base 2 number system, uses only 2 symbols - 0 and 1.  If we were to write a binary number, it might look like the following:

	10010101001012

Starting again with the right-most digit, we have a 1 in the 1s position, or 1 x 20 which is equal to 1 x 1; the next digit 0 is in the 2s position and is 0 x 21; the next digit 1 is in the 4s position and is equal to 1 x 22 or 1 x 4; the next digit 0 is in the 8s position and is equal to 0 x 23, or 0 x 8, and so on.  Notice how the positions start with 1 on the right-most bit and proceed to double for each next position - 1, 2, 4, 8, 16, 32, 64, 128, and so on.  This corresponds to 20, 21, 22, 23, 24, 25, 26, and 27.

Thus, the binary number

	100102

is equal to (starting from the left) 16 + 0 + 0 + 2 + 0, or 18 in decimal.
The binary number

	10111012

is equal to 64 + 0 + 16 + 8 + 4 + 0 + 1, or 93 in decimal.

Base 16 Number System (Hexadecimal)

The binary number system can generate some pretty long strings of 1s and 0s for relatively small equivalent decimal values.  For example, decimal 16492 is equal to binary 100000001101100.  Many people and computer systems often like to work with a notation that is more compact but still closely resembles binary.  One choice is the base 16 number system, or hexadecimal.  Following the same thread of logic, the base 16 number system needs 16 symbols.  The first ten symbols can be the common digits 0 through 9, but what to use for the remaining six symbols?  We will use the symbols A, B, C, D, E, and F.  Thus we use the symbols 0 - F, which are equivalent to the following decimal and binary values:

Hexadecimal	Decimal 	Binary	
0	0	0000	
1	1	0001	
2	2	0010	
3	3	0011	
4	4	0100	
5	5	0101	
6	6	0110	
7	7	0111	
8	8	1000	
9	9	1001	
A	10	1010	
B	11	1011	
C	12	1100	
D	13	1101	
E	14	1110	
F	15	1111	

If we were to write a hexadecimal number as the following:

	C 2 A B16

we would have the following equivalent decimal value: C x 163, or 1210 x 4096; 2 x 162, or 2 x 256; A x 161, or 1010 x 16; and B x 160, or 1110 x 1.  The resulting decimal value would be 49835.

The neat thing about hexadecimal and binary is how easy it is to translate one form into the other.  For example, if we have the hexadecimal value C2AB from above, we can easily convert it to binary by simply replacing each hex digit with the corresponding 4-bit binary equivalent.

C = 11002
2 = 00102
A = 10102
B = 10112
The resulting binary value would be 1100 0010 1010 10112.

Conversely, to convert a binary value to hexadecimal, simply group the binary number in groups of 4 digits (starting from the right) and convert each 4-digit group into its equivalent hexadecimal value.

For example, the binary value

	111 1000 1010 1001 0011 0101 11002

equals    7      8       A      9      3        5       C16

Conversion From One Number System to Another

We have already seen how to do some number conversions.  Binary to hexadecimal and hexadecimal to binary are straightforward.  We have also seen how to convert binary to decimal.  Start at the right side of the binary number and count in powers of 2.  The right-most binary digit is in the 1s position, the next binary digit is in the 2s position, then the 4s position, the 8s position, and so on.

Converting from decimal to binary is also straight-forward.  One simple paper and pencil method is to take the decimal number and repeatedly divide it by 2, keeping track of the integer remainder.  For example, lets convert the decimal value 57 to binary using this method.
	   ____
	2 ) 57
		28 with a remainder of 1	(now divide 28 by 2)
			14 with a remainder of 0
				7 with a remainder of 0
					3 with a remainder of 1
						1 with a remainder of 1
							0 with a remainder of 1.

Now take all the remainders starting with the last one and write your binary number:

	1 1 1 0 0 12

Is this the correct binary value?  Check it.  The left-most bit equals 32, the next bit equals 16, the next bit equals 8, and the right-most bit equals 1.  32 + 16 + 8 + 1 = 57.

You can also convert decimal to hexadecimal in a similar fashion (repeatedly divide by 16 and keep track of the integer remainders) but it might be easier to convert the decimal number to binary first, then simply convert the binary number to hexadecimal.

Binary Addition

It is a fairly common occurrence to add together two binary values.  The rules for binary addition are fairly straightforward.
										   1
	   1			   0			   1			   1
	+ 1			+ 1			+ 0			+ 1
	   0 carry a 1		   1 carry a 0		   1 carry a 0		   1 carry a 1

Lets add the two following binary numbers:

	   01001010010
	+ 10010010101

The result is the following:

	   00111010010
	+ 10110010101
	   11101100111

Integer Representation and Two’s Complement

Computer programs deal with numeric variables.  For example, a Java program may add together two variables as shown in the following:

	int xvalue = 0;
	int yvalue = 0;
	    :
	    :
	xvalue +=  yvalue;
	    :

The variables xvalue and yvalue are integer variables.  An integer is a numeric variable with no fractional part.  It is very common for a programming language or computer to store this integer variable using 32 binary digits, or bits.  If the integer variable xvalue has the decimal value 19, the 32-bit binary form would look like the following:

	00000000 00000000 00000000 000100112

Integers can be positive or negative.  When we write the integer in decimal form, we typically add a negative sign (-) to the front of the integer to denote negative, and usually add nothing to the front of the integer to denote positive.  But how do we represent positive and negative in binary?  In the binary representation, the left-most bit is used as the sign bit.  If the integer is positive, the left most bit is 0.  If the integer is negative, the left most bit is 1.  Thus, the decimal value 19 we just saw moments ago (which was positive) would be correctly displayed in binary as:

	00000000 00000000 00000000 000100112

If the value had been -19, would we simply write the binary form as:

	10000000 00000000 00000000 000100112 ?

As it turns out, it is not this simple.  Most computers store negative values in two’s complement form.  To convert a binary value to its two’s complement form, simply flip all binary 0s to 1s and all 1s to 0s, then add a binary 1.

For example, to represent -19 in two’s complement, start with positive 19:

	00000000 00000000 00000000 000100112

then flip all 0s to 1s and all 1s to 0s (this is called the one's complement):

	11111111 11111111 11111111 111011002	(one’s complement)

then add a 1 to the flipped values:

	11111111 11111111 11111111 11101100	(one’s complement)
	00000000 00000000 00000000 00000001	(a binary 1)
	11111111 11111111 11111111 11101101	(two’s complement form of -1910)

Let’s look at another example.  What if we want to perform the following subtraction:

		 15 - 9 = 6

Instead of performing a subtraction, we would instead add a negative value:

		15 + (-9) = 6

First, let’s find the two’s complement of 9:

	00000000 00000000 00000000 00001001	(positive 9)
	11111111 11111111 11111111 11110110	(one’s complement)
	00000000 00000000 00000000 00000001	(add 1)
	11111111 11111111 11111111 11110111	(negative 9)

Now, let’s add the two values 15 and -9:

	00000000 00000000 00000000 00001111	(15)
	11111111 11111111 11111111 11110111	(-9)
	00000000 00000000 00000000 00000110	(6)

We have added 15 and -9 and our result is 6, as shown above.

Maximum Values

As already noted, a 32-bit binary integer is a very common representation.  With 32 bits, it is possible to have 232 different combinations of values.  What would be the smallest and largest possible values (assuming no sign bit) given a 32-bit storage location?  Zero (0) would be the smallest and 232 - 1 would be the largest.  Given any integer of size n bits and if no sign bit is involved, then 0 is the smallest value and 2n - 1 is the largest.  Let’s consider a very simple example - a 4-bit integer.  With four bits we have 24 or 16 different combinations of values.  The smallest value is 0 and the largest value is 24 - 1, or 15.  Indeed, the largest binary value given four bits is 1111, which is equal to a decimal 15.

What if the integer does include a sign bit?  If the integer has n bits, and one of those bits is used as a sign, then the largest positive value equals 2n-1 - 1.  The only difference is one less bit in the power (the sign bit).  Let’s consider our four bit integer again.  If the left-most bit is a sign bit, then there are only three bits left for the integer value.  The largest positive value is 0111, which is 7.  Or, given a 4-bit signed integer, the largest positive value equals 24-1 - 1.

Floating Point Representation

Floating point values are numbers that have a fractional component.  For example, the following values can be represented as floating point values:

	3.456
	-0.000007
	2.345 x 1012
	3.14159 x 10-9

Floating point values have two parts - the characteristic (exponent or power) and the mantissa (fractional part).  Using the above value 2.345 x 1012, the mantissa is 2.345, and the characteristic is the 1012 part.  Thus, when a floating point value is stored in binary, it is usually stored in two parts:

characteristic	mantissa	

If a 32-bit value is used to store a floating point value, eight bits are allocated to the characteristic while the remaining 24 bits are allocated to the mantissa.

More precisely - the mantissa is usually stored with no digits (other than zero) to the left of the decimal point.  This is called a normalized value.  Thus, 2.345 x 1012 would have to be normalized to 0.2345 x 1013.  Then the 0.2345 is converted to binary and stored in the mantissa.  Of the eight bits in the characteristic, only seven are used for the characteristic.  One bit is the sign bit for the mantissa.  Of the seven remaining characteristic bits, many architectures use an excess code.  For example, if the (the power is 2, excess-64 is added to 2, resulting in 66.  The value 66 is then stored in binary.  If the power was -2, then when 64 is added, the characteristic stored is 62.  This is a clever way to avoid another sign bit.
Character Codes

One of the most common forms of data transmitted between a transmitter and a receiver is textual data.  For example, banking institutions that wish to transfer money often transmit textual information, such as account numbers, names of account owners, bank names, addresses, and the amount of money to be transferred.  This textual information is transmitted as a sequence of characters.  To distinguish one character from another, each character is represented by a unique binary pattern of 1s and 0s.  The set of all textual characters or symbols and their corresponding binary patterns is called a data code.  Two important data codes are EBCDIC and ASCII.

The Extended Binary Coded Decimal Interchange Code, or EBCDIC code, is an 8-bit code allowing 256 possible combinations of textual symbols (28 = 256).  These 256 combinations of textual symbols include all uppercase and lowercase letters, the 10 digits, a large number of special symbols and punctuation marks, and a number of control characters.  The control characters, such as line feed (LF) and carriage return (CR) provide control between a processor and an input/output device.  Certain control characters provide data transfer control between a computer source and computer destination.  All the EBCDIC characters are shown in Figure 1.

Figure 1. The EBCDIC character code set (not shown)

For example, if you want a computer to send the message “Transfer $1200.00" the following EBCDIC characters would be sent:

1110 0011	T
1001 1001	r
1000 0001	a
1001 0101	n
1010 0010	s
1000 0110	f
1000 0101	e
1001 1001	r
0100 0000	space
0101 1011	$
1111 0001	1
1111 0010	2
1111 0000	0
1111 0000	0
0101 1100	.
1111 0000	0
1111 0000	0

The American National Standard Code for Information Interchange (ASCII) is a government standard in the United States and is one of the most widely used data codes in the world.  The ASCII character set comes in a few different forms including a 7-bit version that allows for 128 possible combinations of textual symbols (27 = 128), representing upper and lowercase letters, the digits 0 to 9, special symbols, and control characters.  Since the byte is a common unit of data and consists of eight bits, the 7-bit version of ASCII characters usually includes an eighth bit.  This eighth bit can be used to detect transmission errors, can provide for 128 additional characters defined by the application using the ASCII code set, or can simply be a binary 0.  Figure 2 shows the ASCII character set and the corresponding 7-bit values.

Figure 2.  The ASCII character set (not shown)

Using the example of sending the message “Transfer $1200.00", the corresponding ASCII characters are:

1010100	T
1110010	r
1100001	a
1101110	n
1110011	s
1100110	f
1100101	e
1110010	r
0100000	space
0100100	$
0110001	1
0110010	2
0110000	0
0110000	0
0101110	.
0110000	0
0110000	0

Sample Problems

1. Write the binary equivalent of decimal 149.
2. Write the binary equivalent of decimal 314.
3. Write the hexadecimal equivalent of binary 10100101001010010101.
4. Write the hexadecimal equivalent of binary 001001010111110101101001110.
5. Write the decimal equivalent of binary 100101001010101111.
6. Write the decimal equivalent of binary 00110011101010110.
7. Write the binary equivalent of hexadecimal 3C9AA53E.
8. Write the binary equivalent of hexadecimal 1FFE.
9. What is the binary result of adding 110100101 to 101011110?
10. What is the binary result of adding 1101001 to 0111111?
11. What is the 2s complement of -82?
12. What is the 2s complement of -136?
13. How many combinations in a 64-bit integer?
14. What is the largest positive value given a 64-bit integer?

Solutions to Sample Problems

1. 10010101
2. 100111010
3. A5295
4. 12BEB4E
5. 152239
6. 26454
7. 111100100110101010010100111110
8. 1111111111110
9. 1100000011
10. 10101000
11. 10101110
12. 111101111000
13. 264
14. 263 - 1